package com.dddd.blog.blogsp.task;


import com.dddd.blog.blogsp.pageprocessor.BlogContentPageProcesser;
import com.dddd.blog.blogsp.pipeline.BlogContentPipeline;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

import javax.annotation.Resource;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 喵星球的大橘
 * 获取博客内容的任务类
 */
@Component
public class BlogContentTask {

    private static final String path = System.getProperty("user.dir") + "\\src\\main\\resources\\Author.csv";

    @Resource
    BlogContentPageProcesser blogContentPageProcesser;

    @Resource
    BlogContentPipeline blogContentPipeline;


    /**
     * 名称、链接的键值表
     */
    private static Map<String, String> records;

    /**
     * 加载文件中的名称、链接
     */
    private void loadData() {

        BufferedReader br = null;
        FileInputStream fin = null;
        InputStreamReader inr = null;

        try {
            records = new HashMap<>(10);
            File file = new File(path);
            if (!file.exists()) {
                return;
            }

            fin = new FileInputStream(new File(path));
            inr = new InputStreamReader(fin);
            br = new BufferedReader(inr);

            br.readLine();
            String record;
            String[] items;
            while ((record = br.readLine()) != null && (!"".equals(record))) {
                items = record.split(",");
                records.put(items[0], items[1]);
                System.out.println("author: " + items[0] + " link: " + items[1]);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inr != null) {
                try {
                    inr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fin != null) {
                try {
                    fin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public void requestPost() {

        loadData();

        if (records == null || records.isEmpty()) {
            return;
        }

        for (String author : records.keySet()) {

            String home = records.get(author);
            blogContentPageProcesser.setAuthor(author);
            Spider.create(blogContentPageProcesser)
                    .addUrl(home)
                    .addPipeline(blogContentPipeline)
                    .thread(3)
                    .run();
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }

    }

}